package com.fr.web.core.service;

import com.fr.base.FRContext;
import com.fr.base.core.util.TemplateUtils;
import com.fr.base.dav.DavXMLUtils;
import com.fr.base.dav.Env;
import com.fr.base.file.DatasourceManager;
import com.fr.web.core.WebUtils;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServerProcessorService extends NoSessionIDService
{
  private static ServerProcessorService SERVERPROCESS_SERVICE = new ServerProcessorService();

  public static ServerProcessorService getInstance()
  {
    return SERVERPROCESS_SERVICE;
  }

  public boolean accept(String paramString)
  {
    return paramString.startsWith("server");
  }

  public void process(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String paramString)
    throws Exception
  {
    if ("server".equals(paramString))
      bsServer(paramHttpServletRequest, paramHttpServletResponse);
    if ("server_xml_read".equals(paramString))
      viewServerXML(paramHttpServletRequest, paramHttpServletResponse);
    if ("server_xml_save".equals(paramString))
      saveServerXML(paramHttpServletRequest, paramHttpServletResponse);
    if ("server_sessions_view".equals(paramString))
      SessionDealWith.viewSessions(paramHttpServletRequest, paramHttpServletResponse);
    if ("server_sessions_close".equals(paramString))
      closeSessions(paramHttpServletRequest, paramHttpServletResponse);
    if ("server_config_read".equals(paramString))
      dealWithConfig(paramHttpServletRequest, paramHttpServletResponse);
  }

  private static void bsServer(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    try
    {
      TemplateUtils.dealWithTemplate("/com/fr/web/bmp/FineReportServer.html", paramHttpServletResponse, WebUtils.createTemplateMap(paramHttpServletRequest));
    }
    catch (IOException localIOException)
    {
      FRContext.getLogger().log(Level.WARNING, localIOException.getMessage(), localIOException);
    }
  }

  private static void dealWithConfig(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    RemoteDesignService.read_resource(paramHttpServletRequest, paramHttpServletResponse);
  }

  private static void viewServerXML(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    RemoteDesignService.read_resource(paramHttpServletRequest, paramHttpServletResponse);
  }

  private static void saveServerXML(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "datasource");
    DatasourceManager localDatasourceManager = FRContext.getDatasourceManager();
    ByteArrayInputStream localByteArrayInputStream = new ByteArrayInputStream(str.getBytes());
    if (localByteArrayInputStream != null)
      try
      {
        DavXMLUtils.readXMLDatabaseMapConnection(localByteArrayInputStream);
      }
      catch (Exception localException1)
      {
        FRContext.getLogger().log(Level.WARNING, localException1.getMessage(), localException1);
      }
    Env localEnv = FRContext.getCurrentEnv();
    try
    {
      localEnv.writeResource(localDatasourceManager);
    }
    catch (Exception localException2)
    {
      FRContext.getLogger().log(Level.WARNING, localException2.getMessage(), localException2);
    }
  }

  private static void closeSessions(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "closeSessions");
    String[] arrayOfString = str.split(",");
    for (int i = 0; i < arrayOfString.length; ++i)
      SessionDealWith.closeSession(arrayOfString[i]);
  }
}